package com.study.system.controller;


import com.study.common.result.Result;
import com.study.common.utils.JwtHelper;
import com.study.model.book.Book;
import com.study.model.book.Category;
import com.study.model.vo.BookQueryVo;
import com.study.model.vo.CategoryQueryVo;
import com.study.system.service.CategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author biyu0723
 * @since 2022-10-30
 */
@Api(tags = "图书分类接口")
@RestController
@RequestMapping("/admin/book/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("图书分类列表")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       CategoryQueryVo categoryQueryVo) {
        //创建page对象
        Page<Category> pageParam = new Page<>(page,limit);
        //调用service方法
        IPage<Category> pageModel = categoryService.selectPage(pageParam,categoryQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("添加分类")
    @PostMapping("save")
    public Result save(@RequestBody Category category) {

        category.setCreateTime(new Date());
        category.setIsDeleted(0);
        boolean is_Success = categoryService.save(category);
        if(is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("getCategory/{id}")
    public Result getCategory(@PathVariable String id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    @ApiOperation("修改分类")
    @PostMapping("update")
    public Result update(@RequestBody Category category) {
        category.setUpdateTime(new Date());
        boolean is_Success = categoryService.updateById(category);
        if(is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }



    @ApiOperation("删除分类")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id) {
        boolean is_Success = categoryService.removeById(id);
        if(is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}

